package chu.dejian.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;

import java.util.ArrayList;
import java.util.List;

import chu.dejian.entity.AttendeeEntity;
import chu.dejian.entity.ReceivedRequestEntity;
import chu.dejian.widget.AttentionLevelView;

public class ReceivedRequestDao {
	public static final String RECEIVED_REQUEST_TABLE = "received_request";

	public static final String KEY_AR_ID = "atid";
	public static final String KEY_ATTENDEE = "attendee";
	public static final String KEY_ATTENDEE_NAME = "attendee_name";
	public static final String KEY_ATTENDEE_LEVEL = "attendee_level";
	public static final String KEY_STATU = "statu";

	public static final String RECEIVED_REQUEST_TABLE_CREATE = "CREATE TABLE "
			+ RECEIVED_REQUEST_TABLE + " ("
			+ KEY_AR_ID + " INTEGER PRIMARY KEY, "
			+ KEY_ATTENDEE + " VARCHAR, "
			+ KEY_ATTENDEE_NAME + " VARCHAR, "
			+ KEY_ATTENDEE_LEVEL + " TINYINT, "
			+ KEY_STATU + " TINYINT);";

	public long insertRequest(ReceivedRequestEntity requestEntity) {
		if(getRequest(requestEntity.arId) == null) return DBHelper.getInstance().getDatabase().insert(RECEIVED_REQUEST_TABLE, null, createContentValues(requestEntity));
		else {
			return updateRequest(requestEntity) ? 0 : -1;
		}
	}

	public boolean updateRequest(ReceivedRequestEntity requestEntity) {
		return DBHelper.getInstance().getDatabase().update(RECEIVED_REQUEST_TABLE, createContentValues(requestEntity), KEY_AR_ID + "=" + requestEntity.arId, null) > 0;
	}

	public boolean deleteRequest(int arId) {
		return DBHelper.getInstance().getDatabase().delete(RECEIVED_REQUEST_TABLE, KEY_AR_ID + "=" + arId, null) > 0;
	}

	public List<ReceivedRequestEntity> getAllRequest() {
		Cursor cursor = DBHelper.getInstance().getDatabase().query(RECEIVED_REQUEST_TABLE,null,	null, null, null, null, null);
		List<ReceivedRequestEntity> requestList = new ArrayList<>();
		while (cursor != null && cursor.moveToNext()) {
			ReceivedRequestEntity requestEntity = new ReceivedRequestEntity();
			requestEntity.arId = cursor.getInt(cursor.getColumnIndex(KEY_AR_ID));
			requestEntity.attendeeName = cursor.getString(cursor.getColumnIndex(KEY_ATTENDEE_NAME));
			requestEntity.attendee = cursor.getString(cursor.getColumnIndex(KEY_ATTENDEE));
			requestEntity.attendeeLevel = cursor.getInt(cursor.getColumnIndex(KEY_ATTENDEE_LEVEL));
			requestEntity.status = cursor.getInt(cursor.getColumnIndex(KEY_STATU));
			requestList.add(requestEntity);
		}
		cursor.close();
		return requestList;
	}

	public ReceivedRequestEntity getRequest(int arId) throws SQLException {
		Cursor cursor = DBHelper.getInstance().getDatabase().rawQuery("SELECT * FROM "+ RECEIVED_REQUEST_TABLE + " WHERE "+ KEY_AR_ID +" = ?;",new String[]{arId+""});
		if (cursor != null && cursor.moveToFirst()) {
			ReceivedRequestEntity requestEntity = new ReceivedRequestEntity();
			requestEntity.arId = cursor.getInt(cursor.getColumnIndex(KEY_AR_ID));
			requestEntity.attendeeName = cursor.getString(cursor.getColumnIndex(KEY_ATTENDEE_NAME));
			requestEntity.attendee = cursor.getString(cursor.getColumnIndex(KEY_ATTENDEE));
			requestEntity.attendeeLevel = cursor.getInt(cursor.getColumnIndex(KEY_ATTENDEE_LEVEL));
			requestEntity.status = cursor.getInt(cursor.getColumnIndex(KEY_STATU));
			return requestEntity;
		}
		return null;
	}

	private ContentValues createContentValues(ReceivedRequestEntity requestEntity) {
		ContentValues values = new ContentValues();
		values.put(KEY_AR_ID,requestEntity.arId);
		values.put(KEY_ATTENDEE,requestEntity.attendee);
		values.put(KEY_ATTENDEE_NAME,requestEntity.attendeeName);
		values.put(KEY_ATTENDEE_LEVEL,requestEntity.attendeeLevel);
		values.put(KEY_STATU,requestEntity.status);
		return values;
	}
}
